﻿using System.ComponentModel;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

//Refer to URL:http://msdn.microsoft.com/en-us/library/yhzc935f.aspx

[assembly: TagPrefix("AspDotnetPractice.CustomServerControl", "practiceUI")]
namespace AspDotnetPractice.CustomServerControl
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WelcomeLabel DefaultUserName=\"Rose\" runat=\"server\"></{0}:WelcomeLabel>")]
    [ToolboxBitmap(typeof(ResourceFinder),"AspDotnetPractice.CustomServerControl.welcome.bmp")]
    
    public class WelcomeLabel : Label
    {
        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The text to display when the user is not logged in."),
        Localizable(true)
        ]
        public virtual string DefaultUserName
        {
            get 
            { 
                string defaultUserName = (string) ViewState["DefaultUserName"]; 
                return defaultUserName ?? string.Empty;
            }
            set { ViewState["DefaultUserName"] = value; }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
            string defaultUserName = DefaultUserName;
            if(Context != null)
            {
                string userName = Context.User.Identity.Name;
                if (!string.IsNullOrEmpty(userName))
                {
                    defaultUserName = userName;
                }
            }
            if (!string.IsNullOrEmpty(defaultUserName))
            {
                output.Write(",");
                output.WriteEncodedText(defaultUserName);
            }
            output.Write("!");
        }
    }
}
